/* global hexo */

'use strict';

const { parse } = require('url');
const { unescapeHTML } = require('hexo-util');
const htmlToText = require('html-to-text');

const sanitize = function (post) {
  const content = htmlToText.fromString(
      post,
      {
          ignoreImage: true,
          ignoreHref: true,
          wordwrap: false,
          uppercaseHeadings: false
      }
  );
  return content;
}

hexo.extend.filter.register('after_post_render', data => {
  const { config } = hexo;
  const theme = hexo.theme.config;
  const excerptLength = hexo.config.excerpt_length || 300;
  if(data.excerpt){
    return data;
  }
  const post = sanitize(data.content);
  const excerpt = post.substr(0, excerptLength);
  data.excerpt = excerpt;
  return data;
}, 0);
